package com.github.lee.unsplash.ui.topic

import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.fragment.app.viewModels
import com.github.lee.mvvm.core.databinding.LayoutDefaultLoadingBinding
import com.github.lee.mvvm.core.ui.BaseRefreshFragment
import com.github.lee.unsplash.databinding.ToolbarMainTopicBinding
import com.github.lee.unsplash.ui.search.SearchActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class TopicFragment : BaseRefreshFragment<TopicVM>() {

    private val toolbar by lazy {
        ToolbarMainTopicBinding.inflate(layoutInflater)
            .also {
                it.lifecycleOwner = this
                it.searchClick = { view ->
                    val opt =
                        ActivityOptionsCompat.makeSceneTransitionAnimation(
                            mContext,
                            Pair.create(view, "search_view")
                        )
                    SearchActivity.start(mContext, opt)
                }
            }
    }

    companion object {
        fun newInstance() = TopicFragment()
    }

    //===Desc:=========================================================
    override fun onCreateViewModel(): TopicVM = viewModels<TopicVM>().value

    override fun onCreateToolbar(): Toolbar {
        toolbar.root.layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
        ).also {
            it.topMargin = getStatusBarHeight()
        }
        return toolbar.tl
    }

    override fun onCreateLoading(): View =
        LayoutDefaultLoadingBinding.inflate(layoutInflater).root

    override fun onResume() {
        super.onResume()
        if (!isHidden) {
            changeStatusBarTextColor(true)
        }
    }

    override fun onRefreshOrLoadMore(isRefresh: Boolean) {
        pageVM.refreshOrLodeMore(isRefresh)
    }

}